﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameCode.Models.Home;

namespace GameCode.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        private void loadLayout(LayoutModel model,GameCodeEntities entities)
        {
            var element = (from u in entities.SystemData
                       where u.Name == "Footer"
                       select u).SingleOrDefault();
            if (element == null)
                model.Footer = "";
            else
                model.Footer = element.Value;

            model.FriendLinkList = (from u in entities.PageElement
                                    where u.Type == "FriendLink"
                                    orderby u.Seq, u.Content
                                    select u).ToList();
        }

        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            GameCodeEntities entities = new GameCodeEntities();
            loadLayout(model, entities);
            model.BannerList = (from u in entities.PageElement
                                    where u.Type == "Banner"
                                    orderby u.Seq, u.Content
                                    select u).ToList();

            var element = (from u in entities.SystemData
                           where u.Name == "IPhoneDownload"
                           select u).SingleOrDefault();
            if (element == null)
                model.IPhoneDown = "";
            else
                model.IPhoneDown = element.Value;

            var andriodApp = (from u in entities.AndriodApp
                          orderby u.Version descending
                          select u).FirstOrDefault();
            if (andriodApp == null)
                model.AndriodDown = "#";
            else
                model.AndriodDown = andriodApp.Link;

            return View(model);
        }

        public ActionResult GetLatestAndroidVersion()
        {
            GameCodeEntities entities = new GameCodeEntities();
            var element = (from u in entities.AndriodApp
                           orderby u.Version descending
                           select u).FirstOrDefault();


            if (element == null)
                return this.Json(null,JsonRequestBehavior.AllowGet);
            else
                return this.Json(element, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Product()
        {
            ProductModel model = new ProductModel();
            GameCodeEntities entities = new GameCodeEntities();
            loadLayout(model, entities);

            var article = (from a in entities.SystemArticle
                           where a.Type == "Product"
                           select a).SingleOrDefault();
            if (article != null)
                model.Content = article.Content;
            else
                model.Content = "";

            return View(model);
        }

        public ActionResult Download()
        {
            DownloadModel model = new DownloadModel();
            GameCodeEntities entities = new GameCodeEntities();
            loadLayout(model, entities);

            var article = (from a in entities.SystemArticle
                           where a.Type == "Download"
                           select a).SingleOrDefault();
            if (article != null)
                model.Content = article.Content;
            else
                model.Content = "";

            var element = (from u in entities.SystemData
                           where u.Name == "IPhoneDownload"
                           select u).SingleOrDefault();
            if (element == null)
                model.IPhoneDown = "";
            else
                model.IPhoneDown = element.Value;

            var andriodApp = (from u in entities.AndriodApp
                              orderby u.Version descending
                              select u).FirstOrDefault();
            if (andriodApp == null)
                model.AndriodDown = "#";
            else
                model.AndriodDown = andriodApp.Link;

            element = (from u in entities.SystemData
                       where u.Name == "CodeLink"
                       select u).SingleOrDefault();
            if (element == null)
                model.CodeDown = "";
            else
                model.CodeDown = element.Value;

            return View(model);
        }

        public ActionResult Teamwork()
        {
            TeamworkModel model = new TeamworkModel();
            GameCodeEntities entities = new GameCodeEntities();
            loadLayout(model, entities);

            var article = (from a in entities.SystemArticle
                           where a.Type == "Teamwork"
                           select a).SingleOrDefault();
            if (article != null)
                model.Content = article.Content;
            else
                model.Content = "";

            return View(model);
        }

        public ActionResult Mobile()
        {
            MobileModel model = new MobileModel();
            GameCodeEntities entities = new GameCodeEntities();

            var article = (from a in entities.SystemArticle
                           where a.Type == "Mobile"
                           select a).SingleOrDefault();
            if (article != null)
                model.Content = article.Content;
            else
                model.Content = "";

            var element = (from u in entities.SystemData
                           where u.Name == "IPhoneDownload"
                           select u).SingleOrDefault();
            if (element == null)
                model.IPhoneLink = "";
            else
                model.IPhoneLink = element.Value;

            var andriodApp = (from u in entities.AndriodApp
                              orderby u.Version descending
                              select u).FirstOrDefault();
            if (andriodApp == null)
                model.AndriodLink = "#";
            else
                model.AndriodLink = andriodApp.Link;



            return View(model);
        }
    }
}
